/*
 * InvertedListSerializerTest_gtest.h
 *
 *  Created on: Nov 29, 2012
 *      Author: m
 */

#ifndef INVERTEDLISTSERIALIZERTEST_GTEST_H_
#define INVERTEDLISTSERIALIZERTEST_GTEST_H_

#include <gtest/gtest.h>
#include <cstring>

#include "../physical_layer/serializers/InvertedListSerializer.h"
#include "../physical_layer/file_system/InvertedList.h"

using physical::byte;
using physical::InvertedList;
using physical::InvertedListSerializer;
using physical::InvalidSizeException;

class InvertedListSerializerTestFixture: public ::testing::Test
{
public:
    InvertedListSerializerTestFixture() :
            m_serializer()
    {
        // initialization code here
    }

    void SetUp()
    {
        // code here will execute just before the test ensues
    }

    void TearDown()
    {
        // code here will be called just after the test completes
        // ok to through exceptions from here if need be
    }

    ~InvertedListSerializerTestFixture()
    {
        // cleanup any pending stuff, but no exceptions allowed
    }

    // put in any custom data members that you need
    InvertedListSerializer m_serializer;
};

TEST_F ( InvertedListSerializerTestFixture, serializeDeserializeSequencialList )
{
    InvertedList invertedList = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    InvertedList deserializedInvertedList;
    size_t expectedSerializationBytes;
    size_t serializedBytes;
    size_t deserializedBytes;
    byte buffer[128];

    expectedSerializationBytes = m_serializer.getSize( invertedList );

    serializedBytes = m_serializer.serialize( invertedList, buffer );
    deserializedBytes = m_serializer.deserialize( buffer,
            deserializedInvertedList );

    ASSERT_EQ( expectedSerializationBytes, size_t(22) );
    ASSERT_EQ( serializedBytes, size_t(22) );
    ASSERT_EQ( deserializedBytes, size_t(22) );
    ASSERT_EQ( invertedList, deserializedInvertedList );
}

TEST_F ( InvertedListSerializerTestFixture, serializeDeserializeNonSequencialList )
{
    InvertedList invertedList =
            { 1, 5, 9, 13, 16, 18, 27, 36, 47, 98, 109, 1000 };
    InvertedList deserializedInvertedList;
    size_t expectedSerializationBytes;
    size_t serializedBytes;
    size_t deserializedBytes;
    byte buffer[128];

    expectedSerializationBytes = m_serializer.getSize( invertedList );

    serializedBytes = m_serializer.serialize( invertedList, buffer );
    deserializedBytes = m_serializer.deserialize( buffer,
            deserializedInvertedList );

    ASSERT_EQ( expectedSerializationBytes, size_t(26) );
    ASSERT_EQ( serializedBytes, size_t(26) );
    ASSERT_EQ( deserializedBytes, size_t(26) );
    ASSERT_EQ( invertedList, deserializedInvertedList );
}

TEST_F ( InvertedListSerializerTestFixture, serializeEmptyInvertedList )
{
    InvertedList invertedList;
    byte buffer[128];

    ASSERT_THROW( m_serializer.getSize( invertedList ), InvalidSizeException );
    ASSERT_THROW( m_serializer.serialize( invertedList, buffer ),
            InvalidSizeException );
}

#endif /* INVERTEDLISTSERIALIZERTEST_GTEST_H_ */
